package com.ikas.ai.server.module.equipment.controller;

import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.server.aop.Pageable;
import com.ikas.ai.server.module.AbstractBean;
import com.ikas.ai.server.module.equipment.model.dto.EquipmentAddDTO;
import com.ikas.ai.server.module.equipment.model.dto.EquipmentUpdateDTO;
import com.ikas.ai.server.module.equipment.service.EquipmentService;
import com.ikas.ai.server.module.system.model.LogOpsType;
import com.ikas.ai.utils.NumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 设备资产管理
 *
 * @author tang.xuandong
 * @version 1.0.0
 * @date 2023/2/1 10:49
 */
@RestController
@RequestMapping("/equipment")
public class EquipmentController extends AbstractBean {

    @Autowired
    private EquipmentService equipmentService;

    /**
     * 获取设备资产管理分页数据
     *
     * @param equipmentCode
     * @param equipmentName
     * @param systemNo
     * @return
     */
    @Pageable(sortType = "desc")
    @GetMapping("/page")
    public JsonResult pageQueryEquipment(@RequestParam(value = "equipmentCode", required = false) String equipmentCode,
                                         @RequestParam(value = "equipmentName", required = false) String equipmentName,
                                         @RequestParam(value = "systemNo", required = false) String systemNo) {
        return JsonResult.ok(equipmentService.pageQueryEquipment(equipmentCode, equipmentName, systemNo));
    }


    /**
     * 根据系统id获取所有设备信息
     *
     * @return list
     */
    @GetMapping("/listBySystem/{systemId}")
    public JsonResult getEquipmentsBySystem(@PathVariable("systemId") Long systemId) {
        return JsonResult.ok(equipmentService.getEquipmentsBySystem(systemId));
    }

    /**
     * 获取所有设备信息
     *
     * @return list
     */
    @GetMapping("/list")
    public JsonResult getEquipments(@RequestParam(value = "systemNo", required = false) String systemNo) {
        return JsonResult.ok(equipmentService.getEquipments(systemNo));
    }

    /**
     * 获取所有设备ID和编号
     *
     * @return list
     */
    @GetMapping("/list/simple")
    public JsonResult getAllEquipmentIdAndCode() {
        return JsonResult.ok(equipmentService.getAllEquipmentIdAndCode());
    }

    /**
     * 获取所有设备编号
     *
     * @return list
     */
    @GetMapping("/list/code")
    public JsonResult getAllEquipmentCode(@RequestParam(value = "systemNo", required = false) String systemNo) {
        return JsonResult.ok(equipmentService.getAllEquipmentCode(systemNo));
    }

    /**
     * 新增设备
     *
     * @param equipmentAddDTO
     * @return JsonResult
     */
    @PostMapping("/add")
    public JsonResult addEquipment(@RequestBody @Validated EquipmentAddDTO equipmentAddDTO) {
        return JsonResult.ok(equipmentService.addEquipment(equipmentAddDTO, getUserId()));
    }

    /**
     * 批量新增设备
     *
     * @param equipmentAddDTOS
     * @return
     */
    @PostMapping("/batchAdd")
    public JsonResult batchAddEquipment(@RequestBody @Validated List<EquipmentAddDTO> equipmentAddDTOS) {
        equipmentService.batchAddEquipment(equipmentAddDTOS, getUserId());
        return JsonResult.ok();
    }

    /**
     * 编辑设备
     *
     * @param equipmentUpdateDTO
     * @return JsonResult
     */
    @PostMapping("/update")
    public JsonResult updateEquipment(@RequestBody @Validated EquipmentUpdateDTO equipmentUpdateDTO) {
        return JsonResult.ok(equipmentService.updateEquipment(equipmentUpdateDTO, getUserId()));
    }

    /**
     * 批量编辑设备
     *
     * @param equipmentUpdateDTOS
     * @return
     */
    @PostMapping("/batchUpdate")
    public JsonResult batchUpdateEquipment(@RequestBody @Validated List<EquipmentUpdateDTO> equipmentUpdateDTOS) {
        equipmentService.batchUpdateEquipment(equipmentUpdateDTOS, getUserId());
        return JsonResult.ok();
    }

    /**
     * 删除设备资产管理信息
     *
     * @param request request
     * @param ids     ids
     * @return row
     */
    @PostMapping("/{ids}")
    public JsonResult deleteEquipment(HttpServletRequest request, @PathVariable(name = "ids") String ids) {
        List<Long> equipmentIds = new ArrayList<>();
        for (String id : ids.split(",")) {
            if (!NumUtil.isLong(id, 0, Long.MAX_VALUE)) {
                continue;
            }
            equipmentIds.add(Long.parseLong(id));
        }
        int row = equipmentService.deleteEquipment(equipmentIds);
        this.logOperation(request, LogOpsType.DICT_MANAGE, String.format("删除设备资产管理，设备资产管理ID为[%s]", ids));
        return JsonResult.ok(row);
    }

}
